<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 阿里巴巴找黄金宝箱(I)（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>一贫如洗的樵夫阿里巴巴在去砍柴的路上&#xff0c;无意中发现了强盗集团的藏宝地&#xff0c;藏宝地有编号从0~N的箱子&#xff0c;每个箱子上面贴有一个数字&#xff0c;箱子中可能有一个黄金宝箱。</p> 
<p>黄金宝箱满足排在它之前的所有箱子数字和等于排在它之后的所有箱子数字之和&#xff1b;</p> 
<p>第一个箱子左边部分的数字和定义为0&#xff1b;最后一个箱子右边部分的数字和定义为0.</p> 
<p>请帮阿里巴巴找到黄金宝箱&#xff0c;输出第一个满足条件的黄金宝箱编号&#xff0c;如果不存在黄金宝箱&#xff0c;请返回-1。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>箱子上贴的数字列表&#xff0c;使用逗号分隔&#xff0c;例如1&#xff0c;-1&#xff0c;0<br /> 宝箱的数量不小于1个&#xff0c;不超过10000<br /> 宝箱上贴的数值范围不低于-1000&#xff0c;不超过1000</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>第一个黄金宝箱的编号</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">2,5,-1,8,6</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">3</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;"> <p>下标3之前的数字和为&#xff1a;2 &#43; 5 &#43; -1 &#61; 6</p> <p>下标3之后的数字和为&#xff1a;6 &#61; 6</p> </td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">8,9</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">-1</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">不存在符合要求的位置</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">11</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">0</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;"> <p>下标0之前的数字和为&#xff1a;0</p> <p>下标0之后的数字和为&#xff1a;0</p> </td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题可以利用单指针来解决。</p> 
<p></p> 
<p>我的解题思路如下&#xff1a;</p> 
<p>首先求出输入的数组arr的所有元素之和sum&#xff0c;然后用一个指针 i 从0开始遍历数组arr&#xff0c;假设指针 i 左边元素之和为leftSum&#xff0c;指针 i 右边元素之和为rightSum。</p> 
<p></p> 
<p>那么初始时&#xff0c;即 i &#61; 0时&#xff0c;leftSum &#61; 0&#xff0c; rightSum &#61; sum - arr[0]&#xff0c;如下图&#xff1a;</p> 
<p><img alt="" height="166" src="https://img-blog.csdnimg.cn/40fadbfd422d4a7583dad4576197adcf.png" width="513" /></p> 
<p>如果此时leftSum &#61;&#61; rightSum&#xff0c;那么 i &#61; 0就是宝箱位置。</p> 
<p>否则&#xff0c;继续 i&#43;&#43;&#xff0c;之后 leftSum &#43;&#61; arr[i-1]&#xff0c;rightSum -&#61; arr[i]</p> 
<p><img alt="" height="174" src="https://img-blog.csdnimg.cn/bdd4cb6c6b98431cb204f13b6a9ad5b9.png" width="606" /></p> 
<p>如果 i 走到最后一个元素位置&#xff0c;还没有达成leftSum &#61;&#61; rightSum&#xff0c;那么就返回-1。</p> 
<p></p> 
<p>本题和<a href="https://blog.csdn.net/qfc_128220/article/details/128227864" title="华为OD机试 - 计算数组中心位置&#xff08;Java &amp; JS &amp; Python&#xff09;_伏城之外的博客-CSDN博客">华为OD机试 - 计算数组中心位置&#xff08;Java &amp; JS &amp; Python&#xff09;_伏城之外的博客-CSDN博客</a></p> 
<p>类似&#xff0c;大家可以再试试这题。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int[] arr &#61; Arrays.stream(sc.nextLine().split(&#34;,&#34;)).mapToInt(Integer::parseInt).toArray();

    System.out.println(getResult(arr));
  }

  public static int getResult(int[] arr) {
    int leftSum &#61; 0;
    int rightSum &#61; Arrays.stream(arr).sum() - arr[0];

    if (leftSum &#61;&#61; rightSum) return 0;

    for (int i &#61; 1; i &lt; arr.length; i&#43;&#43;) {
      leftSum &#43;&#61; arr[i - 1];
      rightSum -&#61; arr[i];
      if (leftSum &#61;&#61; rightSum) return i;
    }

    return -1;
  }
}
</code></pre> 
<h4>JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const arr &#61; line.split(&#34;,&#34;).map(Number);
  console.log(getResult(arr));
});

function getResult(arr) {
  let leftSum &#61; 0;
  let rightSum &#61; arr.reduce((a, b) &#61;&gt; a &#43; b) - arr[0];

  if (leftSum &#61;&#61; rightSum) return 0;

  for (let i &#61; 1; i &lt; arr.length; i&#43;&#43;) {
    leftSum &#43;&#61; arr[i - 1];
    rightSum -&#61; arr[i];
    if (leftSum &#61;&#61; rightSum) return i;
  }

  return -1;
}
</code></pre> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
arr &#61; list(map(int, input().split(&#34;,&#34;)))


# 算法入口
def getResult():
    leftSum &#61; 0
    rightSum &#61; sum(arr) - arr[0]

    if leftSum &#61;&#61; rightSum:
        return 0

    for i in range(1, len(arr)):
        leftSum &#43;&#61; arr[i-1]
        rightSum -&#61; arr[i]
        if leftSum &#61;&#61; rightSum:
            return i

    return -1


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;

#define MAX_SIZE 10000

int getResult(int* arr, int arr_size);
int sum(int* arr, int arr_size);

int main()
{
	int arr[MAX_SIZE];
	
	int arr_size &#61; 0;
	while(scanf(&#34;%d&#34;, &amp;arr[arr_size&#43;&#43;])) {
		if(getchar() !&#61; &#39;,&#39;) break;
	}
	
	printf(&#34;%d\n&#34;, getResult(arr, arr_size));
	
	return 0;
}

int getResult(int* arr, int arr_size)
{
	int leftSum &#61; 0;
	int rightSum &#61; sum(arr, arr_size) - arr[0];
	
	if(leftSum &#61;&#61; rightSum) {
		return 0;
	}
	
	for(int i&#61;1; i&lt;arr_size; i&#43;&#43;) {
		leftSum &#43;&#61; arr[i-1];
		rightSum -&#61; arr[i];
		
		if(leftSum &#61;&#61; rightSum) {
			return i;
		}
	}
	
	return -1;
}

int sum(int* arr, int arr_size)
{
	int ans &#61; 0;
	
	for(int i&#61;0; i&lt;arr_size; i&#43;&#43;) {
		ans &#43;&#61; arr[i];
	}
	
	return ans;
}</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>